package com.chess.touch.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class TouchView extends View {

	// private float dx;
	// private float dy;
	private Paint paint;
	private float textSize;
	private float scale = 1.0f;
	final static int MAX_POINTERS = 5;
	private Pointer[] pointers = new Pointer[MAX_POINTERS];
	private GestureDetector gestureDetector;
	private ScaleGestureDetector scaleGestureDetector;

	class Pointer {
		float x = 0;
		float y = 0;
		int index = -1;
		int id = -1;

	}

	public TouchView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public TouchView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TouchView(Context context) {
		super(context);

		for (int i = 0; i < pointers.length; i++) {
			pointers[i] = new Pointer();

		}
		textSize = 16 * getResources().getDisplayMetrics().density;
		paint = new Paint();

		paint.setColor(Color.BLUE);
		paint.setTextSize(textSize);

		gestureDetector = new GestureDetector(context, new ZoomGesture());
		scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGesture());
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		for (Pointer pointer : pointers) {
			if (pointer.index != -1) {
				String text = "Index: " + pointer.index + ", ID: " + pointer.id;
				canvas.drawText(text, pointer.x, pointer.y, paint);
			}
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		gestureDetector.onTouchEvent(event);
		scaleGestureDetector.onTouchEvent(event);

		int pointCounter = Math.min(event.getPointerCount(), MAX_POINTERS);
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_POINTER_DOWN:
		case MotionEvent.ACTION_MOVE:
			for (int i = 0; i < MAX_POINTERS; i++) {
				pointers[i].index = -1;
			}
			for (int i = 0; i < pointCounter; i++) {
				int id = event.getPointerId(i);
				Pointer pointer = pointers[id];
				pointer.index = i;
				pointer.id = id;
				pointer.x = event.getX(i);
				pointer.y = event.getY(i);
			}
			invalidate();
			break;
		case MotionEvent.ACTION_CANCEL:
			for (int i = 0; i < pointCounter; i++) {
				int id = event.getPointerId(i);
				pointers[id].index = -1;
			}
			invalidate();
			break;
		default:
			break;
		}
		return true;
	}

	public class ZoomGesture extends GestureDetector.SimpleOnGestureListener {
		private boolean normal = true;

		@Override
		public boolean onDoubleTap(MotionEvent e) {
			scale = normal ? 3f : 1f;
			paint.setTextSize(scale * textSize);
			normal = !normal;
			invalidate();
			return true;
		}
	}

	public class ScaleGesture extends ScaleGestureDetector.SimpleOnScaleGestureListener {

		@Override
		public boolean onScale(ScaleGestureDetector detector) {
			scale *= detector.getScaleFactor();
			paint.setTextSize(scale * textSize);
			invalidate();
			return true;
		}
	}

}
